package com.example.shirodemo.framework;

import com.demo.result.AjaxResult;
import com.example.shirodemo.framework.shiro.exception.AuthorizationInfoException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * GolabExceptionHandler
 *
 * @author : SPL
 * @since : 2021-06-02 10:22
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * Shiro：登录认证异常
     */
    @ExceptionHandler(AuthenticationException.class)
    public Object ex(AuthenticationException e) {
        log.error("shiro登录认证异常：{}", e.getMessage());
        String msg = e.getCause().getMessage();
        return AjaxResult.error(401, msg);
    }

    /**
     * Shiro：权限认证异常
     */
    @ExceptionHandler(AuthorizationException.class)
    public Object ex(AuthorizationException e) {
        log.error("shiro权限认证异常：{}", e.getMessage());
        return AjaxResult.error(403, "无操作权限");
    }

    @ExceptionHandler(AuthorizationInfoException.class)
    public Object ex(AuthorizationInfoException e) {
        log.error("获取用户授权信息异常", e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    public Object ex(Exception e) {
        log.error("系统错误:", e);
        return AjaxResult.error("系统错误");
    }
}
